/**
 * 倒立方形锥体箭头旋转定位效果
 * @param {Earth} options.earth 渲染容器
 * @param {Array} options.position 箭头位置信息 [ lng, lat, h ]
 * @param {Array} options.color 填充颜色 colorArray
 * @param {Array} options.outlineColor 轮廓颜色 colorArray
 * @param {Array} options.scale 轮廓颜色 colorArray
 * 
 * 
 * @returns {Polygon} Polygon实例
 */

function DrawPosition (options) {
    let radian = Math.PI / 180;
    let position = [
        options.position[0] * radian,
        options.position[1] * radian,
        options.position[2] || 0,
    ]

    // 旋转动画  高低起伏动画
    const preUpdateEvalString = `
        if (typeof p.dAngle === 'undefined') p.dAngle = 0.0;
        p.dAngle += 10.0;
        if (p.dAngle > 360.0) {
            p.dAngle = 0.0;
        }
        p.rotation[0] = p.dAngle / 180.0 * Math.PI;

        if (typeof p.dt === 'undefined') p.dt = 0.0;
        p.dt += 0.01;
        if (p.dt > 1.0) p.dt = 0.0;
        p.position[2] = ${ position[2] + '' } + 2 * Math.sin(p.dt*Math.PI*2.0);
    `;

    const baseConfig = {
        preUpdateEvalString,
        position: position,
        scale: options.scale || [15, 15, 15],
        // 锥形顶点坐标
        positions: [
            1,0,3, 
            0,1,3, 
            -1,0,3, 
            0,-1,3, 
            0, 0, 0
        ],
        renderState: XE.Obj.CustomPrimitive.getRenderState(true, true)
    };

    // 深拷贝 Object.assign baseConfig合并成新对象
    const config1 = Object.assign({}, baseConfig, {
        indices: [0, 1, 4, 1, 2, 4, 2, 3, 4, 3, 0, 4],
        color: options.color || [1, 0, 0, 0.5],
    });

    const config2 = Object.assign({}, baseConfig, {
        indices: [0, 1, 1, 2, 2, 3, 3, 0, 0, 4, 1, 4, 2, 4, 3, 4],
        primitiveType: Cesium.PrimitiveType.LINES,
        color: options.outlineColor || [1.0, 0, 0.0, 1],
    });

    // 填充模型
    var p = new XE.Obj.CustomPrimitive(options.earth);
    p.xbsjFromJSON(config1);

    // 轮廓模型
    var p2 = new XE.Obj.CustomPrimitive(options.earth);
    p2.xbsjFromJSON(config2);

    p._node = this
    p2._node = this
    this.XEObjList = [ p, p2 ]
    this.data = options.data || null
}

DrawPosition.prototype.destroy = function () {
    this.XEObjList.forEach(item => {
        item._node = null
        item.destroy()
    })
    this.XEObjList = []
}

DrawPosition.prototype.setShow = function (options) {
    this.XEObjList.forEach(item => {
        item.show = options
    })
}

export default DrawPosition